package com.lovo.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lovo.dto.StudentPageDTO;
import com.lovo.dto.StudentDTO;
import com.lovo.result.ResponseResult;
import com.lovo.service.StudentService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @title
 * @author: luo
 * @date: 2020/11/21 17:50
 */
@RestController
@RequestMapping("/api/student")
@ResponseResult
public class StudentController {

    @Resource
    private StudentService studentService;

    @PostMapping("/add")
    public void addStudent(@RequestBody StudentDTO studentDTO){
        studentService.addStudent(studentDTO);
    }

    @PostMapping
    public Object findAll(@RequestBody StudentPageDTO page){
//        if (page.getPageSize() == null) {
//            return studentService.fin
//        }
        System.err.println(page);
        PageHelper.startPage(page.getPageNum() == null?1: page.getPageNum(),page.getPageSize()==null?5:page.getPageSize());
        List<StudentDTO> list = studentService.findAll(page);
        PageInfo pageInfo = PageInfo.of(list);
        return pageInfo;
    }

    @DeleteMapping("/{studentId}")
    public void delete(@PathVariable Integer studentId){
        studentService.deleteStudent(studentId);
    }

    @PutMapping("/{studentId}")
    public void update(@PathVariable Integer studentId,@RequestBody StudentDTO studentDTO){
        studentDTO.setStudentId(studentId);
        studentService.updateStudent(studentDTO);
    }

    @GetMapping("/{studentId}")
    public StudentDTO findStudentById(@PathVariable Integer studentId){
        return studentService.findStudentById(studentId);
    }



}
